package com.aptech.action;

import com.aptech.business.CustomerBusiness;
import com.aptech.util.Paging;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetCustomerListAction extends HttpServlet {

    private static final int ROW_PER_PAGE = 4;
    private static final String PAGE = "page";

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String name = request.getParameter("attr_name");
            if (name == null) {
                name = request.getParameter("_name");
            }
            System.out.println("#############################DDD:" + name);

            String page = request.getParameter(PAGE);
            if (page == null || page.equals("")) {
                page = "1";
            }

            List list = CustomerBusiness.getCustomerList(name);
            if (list != null) {
                request.setAttribute("hidCount", list.size());
                request.setAttribute("customer_msg_2", "Total " + list.size() + " records has found.");
            }

            //paging
            int index = (Integer.parseInt(page) - 1) * ROW_PER_PAGE;
            if (list != null && list.size()!= 0) {
            Paging p = new Paging(list, index, ROW_PER_PAGE);
            list = p.getListByIndex();
            request.setAttribute("pages", p.getPages());
            } 
            //end paging
            request.setAttribute("custList", list);
            request.setAttribute("name", name);



            request.getRequestDispatcher("/customer/listCustomers.jsp").forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
            Logger.getLogger(GetCustomerListAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
